/**
 * @file childframeweb.h
 * @brief 
 *
 * 
 *
 * @author 1823419256@qq.com
 * @version 1.0
 * @date 2011
*/

#ifndef MYBOOK_DISPLAY_CHILDFRAMEWEB_H_
#define MYBOOK_DISPLAY_CHILDFRAMEWEB_H_

#include "frame/childframe.h"
#include "webcore/webcore.h"

extern UINT WM_SNB_VIEWCREATED;
extern UINT WM_SNB_GETCONTAINER;

DWORD WINAPI _threadSnbCreateView(LPVOID lpData);

class CChildFrameSnb
	: public CWindowImpl<CChildFrameSnb, CChildFrame, CChildFrameTraits>
	, public CDoubleBufferImpl<CChildFrameSnb>
{
public:
	DECLARE_WND_CLASS(_T("MyChildFrameSnb"))

	CChildFrameSnb()
		: m_hChildView(NULL)
	{
	}

	HWND CreateFrame(HWND hParent, LPCTSTR lpszUrl)
	{
		m_strUrl = lpszUrl;

		CWindowRect rect(hParent);
		rect.MoveToXY(0, 0);

		return Create(hParent, rect);
	}

	void OnFinalMessage(HWND hWnd)
	{
		delete this;
	}

	CString GetUrl() const
	{
		return m_strUrl;
	}

public:
	BEGIN_MSG_MAP(CChildFrameSnb)
		CHAIN_MSG_MAP(CDoubleBufferImpl<CChildFrameSnb>)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_SIZE, OnSize)
		MESSAGE_HANDLER(WM_SETTEXT, OnSetText)
		MESSAGE_HANDLER(WM_SNB_VIEWCREATED, OnSnbViewCreated)
		MESSAGE_HANDLER(WM_SNB_GETCONTAINER, OnSnbGetContainer)
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		DWORD dwThreadID;
		CloseHandle(CreateThread(NULL, 0, _threadSnbCreateView, (LPVOID)m_hWnd, NULL, &dwThreadID));
		return 0;
	}

	LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
	{
		if (m_hChildView)
			::SetWindowPos(m_hChildView, NULL, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), SWP_NOZORDER);

		return 0;
	}

	LRESULT OnSetText(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
	{
		if (lParam != NULL)
		{
 			LPCTSTR lpszTitle = (LPCTSTR)lParam;
 			//SetWindowText(lpszTitle);
 			::SendMessage(GetParent(), WM_SETTEXT, (WPARAM)this, lParam);
		}

		return 0;
	}

	void DoPaint(CDCHandle dc)
	{
	}

	LRESULT OnSnbViewCreated(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
	{
		m_hChildView = (HWND)lParam;

		CClientRect rect(m_hWnd);
		::PostMessage(m_hChildView, WM_SIZE, 0, MAKELPARAM(rect.Width(), rect.Height()));

		if (!m_strUrl.IsEmpty())
		{
			CString strUrl;
			if (_tcsnicmp(m_strUrl, _T("http://"), 7) == 0 || _tcsnicmp(m_strUrl, _T("https://"), 8) == 0 ||
				_tcsnicmp(m_strUrl, _T("file://"), 7) == 0 ||
				_tcsnicmp(m_strUrl, _T("about:"),  6) == 0 ||
				_tcsnicmp(m_strUrl, _T("res://"),  6) == 0 ||
				_tcsnicmp(m_strUrl, _T("snbfile://"),  10) == 0)
			{
				strUrl = m_strUrl;
			}
			else
			{
				strUrl.Format(_T("snbfile://%sdata/%s"), _Module.GetModulePath(), m_strUrl);
				strUrl.Replace(_T("\\"), _T("/"));
			}

			USES_CONVERSION_EX;
			CComBSTR bstrUrl = T2OLE_EX((LPTSTR)(LPCTSTR)strUrl, 0);
			SendMessage(m_hChildView, WM_MYWEB_NAVIGATE, 0, (LPARAM)(BSTR)bstrUrl);
		}

		return 1;
	}

	LRESULT OnSnbGetContainer(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		return (LRESULT)m_hChildView;
	}

public:
	virtual void Accept(CChildFrameInitVisitor* pVistor)
	{
		pVistor->FrameInitVisit(this);
	}

	virtual bool Equal(CChildFrameTypeVisitor* pVistor)
	{
		return pVistor->FrameTypeVisit(this);
	}

protected:
	HWND m_hChildView;
	CString m_strUrl;
};


// CSnbExternalDispatchAdapter
class CSnbExternalDispatchAdapter : public CExternalDispatch
{
public:
	CSnbExternalDispatchAdapter(CWebContainer* pWebContainer);
	~CSnbExternalDispatchAdapter();

public:
	STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId);
	STDMETHOD(Invoke)(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS  *pDispParams, VARIANT  *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
};

// CSnbInternetSecurityManagerAdapter
class CSnbInternetSecurityManagerAdapter : public CInternetSecurityManager
{
public:
	CSnbInternetSecurityManagerAdapter(CWebContainer* pWebContainer);
	~CSnbInternetSecurityManagerAdapter();

public:
	STDMETHOD(MapUrlToZone)(LPCWSTR pwszUrl, DWORD *pdwZone, DWORD dwFlags);
	STDMETHOD(ProcessUrlAction)(LPCWSTR pwszUrl, DWORD dwAction, BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwFlags, DWORD dwReserved);
};

// CSnbCustomSiteAdapter
class CSnbCustomSiteAdapter : public CCustomSite
{
public:
	CSnbCustomSiteAdapter(CWebContainer* pWebContainer);
	~CSnbCustomSiteAdapter();

public:
	STDMETHOD(GetExternal)(IDispatch **ppDispatch);
	STDMETHOD(QueryService)(REFGUID guidService, REFIID riid, void **ppvObject);
	STDMETHOD(ShowMessage)(HWND hwnd, LPOLESTR lpstrText, LPOLESTR lpstrCaption, DWORD dwType, LPOLESTR lpstrHelpFile, DWORD dwHelpContext, LRESULT *plResult);

public:
	CExternalDispatch *m_pExternalDispatch;
};

// CSnbWebEventSinkAdapter
class CSnbWebEventSinkAdapter : public CWebEventSink
{
public:
	CSnbWebEventSinkAdapter(CWebContainer* pWebContainer);
	~CSnbWebEventSinkAdapter();

public:
	virtual HRESULT OnTitleChange(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
	virtual HRESULT OnNewWindow2(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
	virtual HRESULT OnNewWindow3(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
	virtual HRESULT OnBeforeNavigate2(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
	virtual HRESULT OnNavigateError(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
};

// CSnbWebControlExAdapter
class CSnbWebControlExAdapter : public CWebControlEx
{
public:
	CSnbWebControlExAdapter(CWebContainer* pWebContainer);
	~CSnbWebControlExAdapter();

public:
};

class CSnbAdapterCreator : public CIfaceAdapterCreator
{
public:
	virtual CCustomSite* GetCustomSite(CWebContainer* pWebContainer)
	{
		return new CSnbCustomSiteAdapter(pWebContainer);
	}

	virtual CWebEventSink* GetWebEventSink(CWebContainer* pWebContainer)
	{
		return new CSnbWebEventSinkAdapter(pWebContainer);
	}

	virtual CWebControlEx* GetWebControlEx(CWebContainer* pWebContainer)
	{
		return new CSnbWebControlExAdapter(pWebContainer);
	}
};

#endif // MYBOOK_DISPLAY_CHILDFRAMEWEB_H_